package com.car.controller;

import com.car.pojo.RepairAppointment;
import com.car.pojo.RepairAppointmentQueryParam;
import com.car.pojo.Result;
import com.car.service.RepairAppointmentService;
import org.springframework.web.bind.annotation.RequestParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 故障预约管理Controller
 */
@Slf4j
@RequestMapping("/appointments/repair")
@RestController
public class RepairAppointmentController {

    @Autowired
    private RepairAppointmentService repairAppointmentService;


    

    
    /**
     * 分页查询故障预约
     */
    @GetMapping
    public Result page(RepairAppointmentQueryParam param) {
        log.info("分页查询故障预约: {}", param);
        return Result.success(repairAppointmentService.page(param));
    }

    /**
     * 根据ID查询故障预约详情
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Long id) {
        log.info("根据ID查询故障预约详情: {}", id);
        RepairAppointment appointment = repairAppointmentService.getInfo(id);
        return Result.success(appointment);
    }

    /**
     * 更新预约状态和维修员
     */
    @PutMapping("/status")
    public Result updateStatus(@RequestBody Map<String, Object> params) {
        Long id = Long.valueOf(params.get("id").toString());
        String status = (String) params.get("status");
        String empName = (String) params.get("empName");
        log.info("更新故障预约状态和维修员: id={}, status={}, empName={}", id, status, empName);
        repairAppointmentService.updateStatus(id, status, empName);
        return Result.success();
    }

    /**
     * 批量删除故障预约
     */
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids) {
        log.info("批量删除故障预约: {}", ids);
        repairAppointmentService.delete(ids);
        return Result.success();
    }

    /**
     * 查询所有故障预约（用于导出等）
     */
    @GetMapping("/list")
    public Result list(RepairAppointmentQueryParam param) {
        log.info("查询所有故障预约数据: {}", param);
        List<RepairAppointment> list = repairAppointmentService.list(param);
        return Result.success(list);
    }
    
    /**
     * 添加故障预约（用户提交预约）
     */
    @PostMapping
    public Result add(@RequestBody RepairAppointment appointment) {
        log.info("添加故障预约: {}", appointment);
        repairAppointmentService.add(appointment);
        return Result.success();
    }
    
    /**
     * 根据用户ID查询故障预约记录
     */
    @GetMapping("/user")
    public Result listByUserId(@RequestParam Long userId) {
        log.info("接收到根据用户ID查询故障预约记录的请求，用户ID: {}", userId);
        try {
            List<RepairAppointment> list = repairAppointmentService.listByUserId(userId);
            log.info("查询结果: 找到 {} 条记录", list != null ? list.size() : 0);
            return Result.success(list);
        } catch (Exception e) {
            log.error("查询故障预约记录失败", e);
            return Result.error("查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据用户ID统计故障预约数量
     */
    @GetMapping("/user/count")
    public Result countByUserId(@RequestParam Long userId) {
        log.info("接收到根据用户ID统计故障预约数量的请求，用户ID: {}", userId);
        try {
            Integer count = repairAppointmentService.countByUserId(userId);
            log.info("统计结果: {} 条记录", count);
            return Result.success(count);
        } catch (Exception e) {
            log.error("统计故障预约数量失败", e);
            return Result.error("统计失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据员工名称参数查询维修预约
     */
    @GetMapping("/by-emp-name")
    public Result listByEmpNameParam(@RequestParam String empName) {
        log.info("根据员工名称参数查询维修预约: empName={}", empName);
        List<RepairAppointment> appointments = repairAppointmentService.listByEmpName(empName);
        return Result.success(appointments);
    }
    
    /**
     * 获取空闲维修员列表
     * 查询job_id=2且status='空闲'的员工
     */
    @GetMapping("/repair-staff")
    public Result getRepairStaff() {
        log.info("获取空闲维修员列表");
        List<Map<String, Object>> staffList = repairAppointmentService.getRepairStaff();
        return Result.success(staffList);
    }
    
    /**
     * 统计当前登录员工所关联的状态为'已确认'的维修预约数量
     */
    @GetMapping("/confirmed-count")
    public Result countConfirmedByEmpName(@RequestParam String empName) {
        log.info("统计当前员工已确认维修预约数量: empName={}", empName);
        Integer count = repairAppointmentService.countConfirmedByEmpName(empName);
        return Result.success(count);
    }
    
    /**
     * 统计当前登录员工所关联的状态为'已完成'的维修预约数量
     */
    @GetMapping("/completed-count")
    public Result countCompletedByEmpName(@RequestParam String empName) {
        log.info("统计当前员工已完成维修预约数量: empName={}", empName);
        Integer count = repairAppointmentService.countCompletedByEmpName(empName);
        return Result.success(count);
    }
}